import { router } from '../../App'
import 'reflect-metadata'
import { Methods } from '.'

export function Controller(prefix: string = '') {
    return (target: any) => {
        const prefixPath = prefix
        for(let key in target.prototype){
            const path = Reflect.getMetadata('path', target.prototype, key)
            const method: Methods = Reflect.getMetadata('method', target.prototype, key)
            const middleware = Reflect.getMetadata('middleware', target.prototype, key)
            const handler = target.prototype[key]
            if(path && method && handler) {
                middleware ? router[method](prefixPath + path, middleware, handler)
                : router[method](prefixPath + path, handler)
            }
        }
    }
}
